package com.itheima.dao.impl;

import com.itheima.dao.AdminDao;
import com.itheima.domain.Admin;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AdminDaoImpl2 extends JdbcDaoSupport implements AdminDao {

    @Override
    public Admin findById(Integer adminId) {
        String sql = "select * from admin where id = ?";
        List<Admin> list = getJdbcTemplate().query(
                sql,
                new BeanPropertyRowMapper<Admin>(Admin.class),
                adminId
        );
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public Admin findByName(String adminName) {
        String sql = "select * from admin where username = ?";
        List<Admin> list = getJdbcTemplate().query(
                sql,
                new BeanPropertyRowMapper<Admin>(Admin.class),
                adminName
        );
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public int updateAdmin(Admin admin) {
        String sql = "update admin set username = ?, address = ?, money = ? where id = ?";
        return getJdbcTemplate().update(
                sql,
                admin.getUsername(),
                admin.getAddress(),
                admin.getMoney(),
                admin.getId()
        );
    }
}
